package com.yonyou.crm.sact.outwork.ctrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sact.outwork.entity.OutworkRuleVO;
import com.yonyou.crm.sact.outwork.rmitf.IOutworkRmService;

@RestController
@RequestMapping(value="/sact/outworks")
public class OutworkCtrl {

	@Autowired
	private IOutworkRmService service;
	
	/*
	 * 验证规则时间格式是否正确
	 */
	private String validateRuleTime(String startTime, String endTime) {
		if (startTime == null | startTime.isEmpty() || endTime == null || endTime.isEmpty()) {
			return "起止时间不能为空";
		}
		if (!startTime.contains(":") || !endTime.contains(":")) {
			return "时间格式不正确";
		}
		String[] startTimeStrings = startTime.split(":");
		if (startTimeStrings.length != 2) {
			return "时间格式不正确";
		}
		if (startTimeStrings[0].length() > 2 || startTimeStrings[0].length() < 1) {
			return "时间格式不正确";
		}
		if (startTimeStrings[1].length() != 2) {
			return "时间格式不正确";
		}
		String[] endTimeStrings = endTime.split(":");
		if (endTimeStrings.length != 2) {
			return "时间格式不正确";
		}
		if (endTimeStrings[0].length() > 2 || endTimeStrings[0].length() < 1) {
			return "时间格式不正确";
		}
		if (endTimeStrings[1].length() != 2) {
			return "时间格式不正确";
		}
		return "";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.POST)
	public Object saveRule(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		Integer enableState = Integer.parseInt(paramObj.getString("enableState"));
		List<Map<String, String>> ruleTimeList = (List<Map<String, String>>)paramObj.get("ruleTimeList");
		
		StringBuffer timeRange = new StringBuffer();
		for (Map<String, String> rule : ruleTimeList) {
			String startTime = rule.get("starttime");
			String endTime = rule.get("endtime");
			String validMsg = validateRuleTime(startTime, endTime);
			if (!validMsg.isEmpty()) {
				throw new CrmMessageException(PCCodeConstant.ServiceFormVaild, validMsg);
			}
			if (startTime.length() == 4) {
				startTime = "0" + startTime;
			}
			if (endTime.length() == 4) {
				endTime = "0" + endTime;
			}
			if (timeRange.length() > 0) {
				timeRange.append(",");
			}
			timeRange.append(startTime + "-" + endTime);
		}
		
		OutworkRuleVO vo = new OutworkRuleVO();
		vo.setEnableState(enableState);
		vo.setTimeRange(timeRange.toString());
		
		Map<String, Object> rsMap = service.saveRule(vo);
		return ResponseUtil.getObject(rsMap);
	}
	
	@RequestMapping(value = "rules", method = RequestMethod.GET)
	public Object getRule(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		Object dateObj = paramObj.get("date");
		Map<String, Object> paraMap = new HashMap<String, Object>();

		if (dateObj == null) {
			Date today = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			paraMap.put("date", sdf.format(today));
		} else {
			paraMap.put("date", String.valueOf(dateObj));
		}
		
		Map<String, Object> ruleList = service.getRule(paraMap);
		
		return ResponseUtil.getObject(ruleList);
	}
}
